<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Knowledge Graph Completion &mdash; Graph4NLP v0.4.1 documentation</title><link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
  <!--[if lt IE 9]>
    <script src="../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  <script id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/underscore.js"></script>
        <script src="../../_static/doctools.js"></script>
        <script src="../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <script src="../../_static/js/theme.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Chapter 7. Evaluations and Loss components" href="../evaluation.html" />
    <link rel="prev" title="Link Prediction" href="link_prediction.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
            <a href="../../index.html" class="icon icon-home"> Graph4NLP
          </a>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <p class="caption"><span class="caption-text">Get Started</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../welcome/installation.html">Install Graph4NLP</a></li>
</ul>
<p class="caption"><span class="caption-text">User Guide</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../graphdata.html">Chapter 1. Graph Data</a></li>
<li class="toctree-l1"><a class="reference internal" href="../dataset.html">Chapter 2. Dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../construction.html">Chapter 3. Graph Construction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../gnn.html">Chapter 4. Graph Encoder</a></li>
<li class="toctree-l1"><a class="reference internal" href="../decoding.html">Chapter 5. Decoder</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../classification.html">Chapter 6. Classification</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="node_classification.html">Node Classification</a></li>
<li class="toctree-l2"><a class="reference internal" href="graph_classification.html">Graph Classification</a></li>
<li class="toctree-l2"><a class="reference internal" href="link_prediction.html">Link Prediction</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Knowledge Graph Completion</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#distmult">DistMult</a></li>
<li class="toctree-l3"><a class="reference internal" href="#complex">ComplEx</a></li>
<li class="toctree-l3"><a class="reference internal" href="#how-to-combine-kgc-decoder-with-gnn-encoder">How to Combine KGC Decoder with GNN Encoder</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../evaluation.html">Chapter 7. Evaluations and Loss components</a></li>
</ul>
<p class="caption"><span class="caption-text">Module API references</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../modules/data.html">graph4nlp.data</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules/datasets.html">graph4nlp.datasets</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules/graph_construction.html">graph4nlp.graph_construction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules/graph_embedding.html">graph4nlp.graph_embedding</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules/prediction.html">graph4nlp.prediction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules/loss.html">graph4nlp.loss</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules/evaluation.html">graph4nlp.evaluation</a></li>
</ul>
<p class="caption"><span class="caption-text">Tutorials</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../tutorial/text_classification.html">Text Classification Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../tutorial/semantic_parsing.html">Semantic Parsing Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../tutorial/math_word_problem.html">Math Word Problem Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../tutorial/knowledge_graph_completion.html">Knowledge Graph Completion Tutorial</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">Graph4NLP</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../../index.html" class="icon icon-home"></a> &raquo;</li>
          <li><a href="../classification.html">Chapter 6. Classification</a> &raquo;</li>
      <li>Knowledge Graph Completion</li>
      <li class="wy-breadcrumbs-aside">
            <a href="../../_sources/guide/classification/kgcompletion.rst.txt" rel="nofollow"> View page source</a>
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <div class="section" id="knowledge-graph-completion">
<span id="guide-kgcompletion"></span><h1>Knowledge Graph Completion<a class="headerlink" href="#knowledge-graph-completion" title="Permalink to this headline">¶</a></h1>
<p>The purpose of Knowledge Graph Completion (KGC) is to predict new triples on the basis of existing triples,
so as to further extend KGs. KGC is usually considered as a link prediction task.
Formally, the knowledge graph is represented by <span class="math notranslate nohighlight">\(\mathcal{G} = (\mathcal{V}, \mathcal{E}, \mathcal{R})\)</span>,
in which entities <span class="math notranslate nohighlight">\(v_i \in \mathcal{V}\)</span>, edges <span class="math notranslate nohighlight">\((v_s, r, v_o) \in \mathcal{E}\)</span>,
and <span class="math notranslate nohighlight">\(r \in \mathcal{R}\)</span> is a relation type. This task scores for new facts
(i.e. triples like <span class="math notranslate nohighlight">\(\left \langle subject, relation, object \right \rangle\)</span>) to
determine how likely those edges are to belong to <span class="math notranslate nohighlight">\(\mathcal{E}\)</span>.</p>
<p>KGC can be solved with an encoder-decoder framework. To encode the local neighborhood
information of an entity, the encoder can be chosen from a variety of GNNs.</p>
<p>The decoder is a knowledge graph embedding model and can be regarded as a scoring
function. The most common decoders of knowledge graph completion includes
translation-based models (TransE), tensor factorization based models (DistMult,
ComplEx) and neural network base models (ConvE).
We implement <code class="docutils literal notranslate"><span class="pre">DistMult</span></code> and <code class="docutils literal notranslate"><span class="pre">ComplEx</span></code> in this library.</p>
<div class="section" id="distmult">
<h2>DistMult<a class="headerlink" href="#distmult" title="Permalink to this headline">¶</a></h2>
<p>DistMult is a tensor factorization based models from paper <a class="reference external" href="https://arxiv.org/pdf/1412.6575.pdf">Embedding entities and
relations for learning and inference in knowledge bases</a>.
For <code class="docutils literal notranslate"><span class="pre">DistMult</span></code>, the equation is:</p>
<div class="math notranslate nohighlight">
\[f(s, r, o) = e_s^T M_r e_o,
e_s, e_o \in \mathbb{R}^d,
M_r \in \mathbb{R}^{d \times d}\]</div>
<p>In DistMult, every relation r is represented by a diagonal matrix <span class="math notranslate nohighlight">\(M_r \in \mathbb{R}^{d \times d}\)</span>
and a triple is scored as <span class="math notranslate nohighlight">\(f(s, r, o) = e_s^T M_r e_o\)</span>.</p>
<p>In our implementation, the subject embedding, relation embedding and all entity
embeddings are given as the <code class="docutils literal notranslate"><span class="pre">forward(...)</span></code> input. Then, we compute the score logits
for all entity nodes using multi-class loss such as <code class="docutils literal notranslate"><span class="pre">BCELoss()</span></code> or the predition
scores of positive/negative examples using pairwise Loss Function such as <code class="docutils literal notranslate"><span class="pre">SoftplusLoss()</span></code>
and <code class="docutils literal notranslate"><span class="pre">SigmoidLoss()</span></code>. More details about the KG completion loss please refer to <span class="xref std std-ref">graph4nlp.loss.KGLoss</span>.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">DistMult</span><span class="p">(</span><span class="n">KGCompletionBase</span><span class="p">):</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
                 <span class="n">input_dropout</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span>
                 <span class="n">loss_name</span><span class="o">=</span><span class="s1">&#39;BCELoss&#39;</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">DistMult</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">loss_name</span> <span class="o">=</span> <span class="n">loss_name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">classifier</span> <span class="o">=</span> <span class="n">DistMultLayer</span><span class="p">(</span><span class="n">input_dropout</span><span class="p">,</span> <span class="n">loss_name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">input_graph</span><span class="p">:</span> <span class="n">GraphData</span><span class="p">,</span> <span class="n">e1_emb</span><span class="p">,</span> <span class="n">rel_emb</span><span class="p">,</span> <span class="n">all_node_emb</span><span class="p">,</span> <span class="n">multi_label</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>

        <span class="k">if</span> <span class="n">multi_label</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">input_graph</span><span class="o">.</span><span class="n">graph_attributes</span><span class="p">[</span><span class="s1">&#39;logits&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">classifier</span><span class="p">(</span><span class="n">e1_emb</span><span class="p">,</span>
                                                                     <span class="n">rel_emb</span><span class="p">,</span>
                                                                     <span class="n">all_node_emb</span><span class="p">)</span>  <span class="c1"># [B, N]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">input_graph</span><span class="o">.</span><span class="n">graph_attributes</span><span class="p">[</span><span class="s1">&#39;logits&#39;</span><span class="p">],</span> <span class="n">input_graph</span><span class="o">.</span><span class="n">graph_attributes</span><span class="p">[</span><span class="s1">&#39;p_score&#39;</span><span class="p">],</span> \
            <span class="n">input_graph</span><span class="o">.</span><span class="n">graph_attributes</span><span class="p">[</span><span class="s1">&#39;n_score&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">classifier</span><span class="p">(</span><span class="n">e1_emb</span><span class="p">,</span>
                                                                      <span class="n">rel_emb</span><span class="p">,</span>
                                                                      <span class="n">all_node_emb</span><span class="p">,</span>
                                                                      <span class="n">multi_label</span><span class="p">)</span>
            <span class="c1"># input_graph.graph_attributes[&#39;p_score&#39;]: [L_p]</span>
            <span class="c1"># input_graph.graph_attributes[&#39;n_score&#39;]: [L_n]</span>
            <span class="c1"># L_p + L_n == B * N</span>

        <span class="k">return</span> <span class="n">input_graph</span>


<span class="k">class</span> <span class="nc">DistMultLayer</span><span class="p">(</span><span class="n">KGCompletionLayerBase</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
                 <span class="n">input_dropout</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span>
                 <span class="n">loss_name</span><span class="o">=</span><span class="s1">&#39;BCELoss&#39;</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">DistMultLayer</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">inp_drop</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Dropout</span><span class="p">(</span><span class="n">input_dropout</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">loss_name</span> <span class="o">=</span> <span class="n">loss_name</span>

    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
                <span class="n">e1_emb</span><span class="p">,</span>
                <span class="n">rel_emb</span><span class="p">,</span>
                <span class="n">all_node_emb</span><span class="p">,</span>
                <span class="n">multi_label</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>

        <span class="c1"># dropout</span>
        <span class="n">e1_emb</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">inp_drop</span><span class="p">(</span><span class="n">e1_emb</span><span class="p">)</span>
        <span class="n">rel_emb</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">inp_drop</span><span class="p">(</span><span class="n">rel_emb</span><span class="p">)</span>

        <span class="n">logits</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">mm</span><span class="p">(</span><span class="n">e1_emb</span> <span class="o">*</span> <span class="n">rel_emb</span><span class="p">,</span>
                          <span class="n">all_node_emb</span><span class="o">.</span><span class="n">weight</span><span class="o">.</span><span class="n">transpose</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span>


        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">loss_name</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;SoftMarginLoss&#39;</span><span class="p">]:</span>
            <span class="c1"># target labels are numbers selecting from -1 and 1.</span>
            <span class="n">pred</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">tanh</span><span class="p">(</span><span class="n">logits</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># target labels are numbers selecting from 0 and 1.</span>
            <span class="n">pred</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">sigmoid</span><span class="p">(</span><span class="n">logits</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">multi_label</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">idxs_pos</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">nonzero</span><span class="p">(</span><span class="n">multi_label</span> <span class="o">==</span> <span class="mf">1.</span><span class="p">)</span>
            <span class="n">pred_pos</span> <span class="o">=</span> <span class="n">pred</span><span class="p">[</span><span class="n">idxs_pos</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">],</span> <span class="n">idxs_pos</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">]]</span>

            <span class="n">idxs_neg</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">nonzero</span><span class="p">(</span><span class="n">multi_label</span> <span class="o">==</span> <span class="mf">0.</span><span class="p">)</span>
            <span class="n">pred_neg</span> <span class="o">=</span> <span class="n">pred</span><span class="p">[</span><span class="n">idxs_neg</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">],</span> <span class="n">idxs_neg</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">]]</span>
            <span class="k">return</span> <span class="n">pred</span><span class="p">,</span> <span class="n">pred_pos</span><span class="p">,</span> <span class="n">pred_neg</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">pred</span>
</pre></div>
</div>
</div>
<div class="section" id="complex">
<h2>ComplEx<a class="headerlink" href="#complex" title="Permalink to this headline">¶</a></h2>
<p>ComplEx is proposed in paper <a class="reference external" href="http://proceedings.mlr.press/v48/trouillon16.pdf">Complex Embeddings for Simple Link Prediction</a>.
For <code class="docutils literal notranslate"><span class="pre">ComplEx</span></code>, the equation is:</p>
<div class="math notranslate nohighlight">
\[\text{Re}(e_r, e_s, \bar{e_t})=\text{Re}(\sum_{k=1}^K e_r e_s\bar{e_t})
, e_s, e_o \in \mathbb{C}^d
, e_r \in \mathbb{C}^d\]</div>
<p><span class="math notranslate nohighlight">\(Re()\)</span> denotes the real part of a vector.</p>
</div>
<div class="section" id="how-to-combine-kgc-decoder-with-gnn-encoder">
<h2>How to Combine KGC Decoder with GNN Encoder<a class="headerlink" href="#how-to-combine-kgc-decoder-with-gnn-encoder" title="Permalink to this headline">¶</a></h2>
<p>The code below provides an end-to-end KGC model using <code class="docutils literal notranslate"><span class="pre">GCN</span></code> as encoder and <code class="docutils literal notranslate"><span class="pre">DistMult</span></code> as decoder:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">graph4nlp.pytorch.modules.graph_embedding.gcn</span> <span class="kn">import</span> <span class="n">GCN</span>
<span class="kn">from</span> <span class="nn">graph4nlp.pytorch.modules.prediction.classification.kg_completion</span> <span class="kn">import</span> <span class="n">DistMult</span>
<span class="kn">from</span> <span class="nn">torch.nn.init</span> <span class="kn">import</span> <span class="n">xavier_normal_</span>

<span class="k">class</span> <span class="nc">GCNDistMult</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">num_entities</span><span class="p">,</span> <span class="n">num_relations</span><span class="p">,</span> <span class="n">num_layers</span><span class="o">=</span><span class="mi">2</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">GCNDistMult</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">emb_e</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">Embedding</span><span class="p">(</span><span class="n">num_entities</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">embedding_dim</span><span class="p">,</span> <span class="n">padding_idx</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">emb_rel</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">Embedding</span><span class="p">(</span><span class="n">num_relations</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">embedding_dim</span><span class="p">,</span> <span class="n">padding_idx</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">num_entities</span> <span class="o">=</span> <span class="n">num_entities</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">num_relations</span> <span class="o">=</span> <span class="n">num_relations</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">num_layers</span> <span class="o">=</span> <span class="n">num_layers</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">gnn</span> <span class="o">=</span> <span class="n">GCN</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">num_layers</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">embedding_dim</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">embedding_dim</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">embedding_dim</span><span class="p">,</span>
                       <span class="n">args</span><span class="o">.</span><span class="n">direction_option</span><span class="p">,</span> <span class="n">feat_drop</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">input_drop</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">direction_option</span> <span class="o">=</span> <span class="n">args</span><span class="o">.</span><span class="n">direction_option</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">distmult</span> <span class="o">=</span> <span class="n">DistMult</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">input_drop</span><span class="p">,</span> <span class="n">loss_name</span><span class="o">=</span><span class="s1">&#39;BCELoss&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">loss</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">BCELoss</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">init</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">xavier_normal_</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">emb_e</span><span class="o">.</span><span class="n">weight</span><span class="o">.</span><span class="n">data</span><span class="p">)</span>
        <span class="n">xavier_normal_</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">emb_rel</span><span class="o">.</span><span class="n">weight</span><span class="o">.</span><span class="n">data</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">e1</span><span class="p">,</span> <span class="n">rel</span><span class="p">,</span> <span class="n">kg_graph</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">X</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">LongTensor</span><span class="p">([</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">num_entities</span><span class="p">)])</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="n">e1</span><span class="o">.</span><span class="n">device</span><span class="p">)</span>

        <span class="n">kg_graph</span><span class="o">.</span><span class="n">node_features</span><span class="p">[</span><span class="s1">&#39;node_feat&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">emb_e</span><span class="p">(</span><span class="n">X</span><span class="p">)</span>
        <span class="n">kg_graph</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">gnn</span><span class="p">(</span><span class="n">kg_graph</span><span class="p">)</span>

        <span class="n">e1_embedded</span> <span class="o">=</span> <span class="n">kg_graph</span><span class="o">.</span><span class="n">node_features</span><span class="p">[</span><span class="s1">&#39;node_feat&#39;</span><span class="p">][</span><span class="n">e1</span><span class="p">]</span>
        <span class="n">rel_embedded</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">emb_rel</span><span class="p">(</span><span class="n">rel</span><span class="p">)</span>
        <span class="n">e1_embedded</span> <span class="o">=</span> <span class="n">e1_embedded</span><span class="o">.</span><span class="n">squeeze</span><span class="p">()</span>
        <span class="n">rel_embedded</span> <span class="o">=</span> <span class="n">rel_embedded</span><span class="o">.</span><span class="n">squeeze</span><span class="p">()</span>

        <span class="n">kg_graph</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">distmult</span><span class="p">(</span><span class="n">kg_graph</span><span class="p">,</span> <span class="n">e1_embedded</span><span class="p">,</span> <span class="n">rel_embedded</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">emb_e</span><span class="p">)</span>
        <span class="n">logits</span> <span class="o">=</span> <span class="n">kg_graph</span><span class="o">.</span><span class="n">graph_attributes</span><span class="p">[</span><span class="s1">&#39;logits&#39;</span><span class="p">]</span>

        <span class="k">return</span> <span class="n">logits</span>
</pre></div>
</div>
</div>
</div>


           </div>
          </div>
          <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
        <a href="link_prediction.html" class="btn btn-neutral float-left" title="Link Prediction" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
        <a href="../evaluation.html" class="btn btn-neutral float-right" title="Chapter 7. Evaluations and Loss components" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2020, Graph4AI Group.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>